package com.wss.module.wan.ui.wxnumber;

import com.wss.common.adapter.FragmentPagerAdapter;
import com.wss.common.base.BaseActionBarAbility;
import com.wss.common.base.BaseFragment;
import com.wss.common.bean.HorizontalTabTitle;
import com.wss.common.widget.PagerSlidingTabStrip;
import com.wss.module.wan.ResourceTable;
import com.wss.module.wan.bean.WXNumber;
import com.wss.module.wan.ui.wxnumber.fragment.WXArticleFragment;
import com.wss.module.wan.ui.wxnumber.mvp.WXNumberPresenter;
import com.wss.module.wan.ui.wxnumber.mvp.contract.WXNumberContract;
import ohos.aafwk.content.Intent;
import ohos.agp.components.PageSlider;
import java.util.ArrayList;
import java.util.List;

public class WXNumberAbility extends BaseActionBarAbility<WXNumberPresenter> implements WXNumberContract.View {

    private PagerSlidingTabStrip tabStrip;
    private PageSlider viewPager;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        initView();
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_wxnumber;
    }

    @Override
    protected WXNumberPresenter createPresenter() {
        return new WXNumberPresenter();
    }

    @Override
    public void refreshWxNumber(List<WXNumber> numberList) {
        List<HorizontalTabTitle> tabTitleList = new ArrayList<>();
        List<BaseFragment> fragments = new ArrayList<>();

        for (WXNumber wx : numberList) {
            tabTitleList.add(new HorizontalTabTitle<>(wx.getName(), wx));
            fragments.add(new WXArticleFragment(this));
        }
        FragmentPagerAdapter mAdapter = new FragmentPagerAdapter(tabTitleList, fragments);
        viewPager.setProvider(mAdapter);
        tabStrip.setViewPager(viewPager);

        tabStrip.setOnPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {
                if (fragments != null && position < fragments.size()) {
                    BaseFragment fragment = fragments.get(position);
                    if (fragment != null && (fragment instanceof WXArticleFragment)) {
                        ((WXArticleFragment) fragment).getPresenter().start();
                    }
                }
            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {

            }
        });

        if (fragments != null && fragments.size() > 0) {
            BaseFragment fragment = fragments.get(0);
            if (fragment != null && (fragment instanceof WXArticleFragment)) {
                ((WXArticleFragment) fragment).getPresenter().start();
            }
        }
    }

    private void initView() {
        setCenterText("微信公众号");

        tabStrip = (PagerSlidingTabStrip) findComponentById(ResourceTable.Id_pst_tab);
        viewPager = (PageSlider) findComponentById(ResourceTable.Id_vp_list);
        getPresenter().start();
    }
}
